/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrameResultados.java
 *
 * Created on 27/Mai/2012, 10:12:28
 */
package smallsimulator;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import org.jdesktop.application.Action;
import simulador.Servico;
import simulador.ServicoStats;
import simulador.Simulador;

/**
 *
 * @author Old
 */
public class FrameResultados extends javax.swing.JInternalFrame {

    private ArrayList<Object> simuList;
    private ArrayList<ConstrucaoGrafico> grafs = new ArrayList<ConstrucaoGrafico>();
    private String nome = "";

    /** Creates new form FrameResultados */
    public FrameResultados(ArrayList<Object> infos, String nomeDaSimulacao) {
        initComponents();
        simuList = infos;
        nome = nomeDaSimulacao;
        runSimulation();
    }

    public void runSimulation() {
        Simulador runner = new Simulador(simuList);
        runner.executa();

        resultadosArea.setText(runner.getRelat());

        ArrayList<ServicoStats> stats = runner.getSerStats();
        ArrayList<Servico> servs = runner.getServicosIds();

        grafs.clear();
        
        for (int s = 0; s < servs.size(); s++) {
            ConstrucaoGrafico graf = new ConstrucaoGrafico("linhas");
            grafs.add(graf);
            graf.setTituloGrafico("Gráfico report serviço " + ((ServicoStats) stats.get(s)).getNomeServico());
            graf.setEixoXLabel("tempo");
            graf.setEixoYLabel("valor");
            String[] nn = new String[6];
            nn[0] = "Fila Máximo";
            nn[1] = "Fila Size";
            nn[2] = "Tempo Médio";
            nn[3] = "Comprimento Médio";
            nn[4] = "Utilização";
            nn[5] = "Atendidos";
            graf.setnRegistos(6, nn);

            ArrayList<Float> Farr = ((ServicoStats) stats.get(s)).getFilamax();
            float[] floatArr = new float[Farr.size()];
            for (int it = 0; it < Farr.size(); it++) {
                floatArr[it] = Farr.get(it).floatValue();
            }
            graf.addValores(floatArr, 0);


            Farr = ((ServicoStats) stats.get(s)).getFila();
            floatArr = new float[Farr.size()];
            for (int it = 0; it < Farr.size(); it++) {
                floatArr[it] = Farr.get(it).floatValue();
            }
            graf.addValores(floatArr, 1);

            Farr = ((ServicoStats) stats.get(s)).getTempomed();
            floatArr = new float[Farr.size()];
            for (int it = 0; it < Farr.size(); it++) {
                floatArr[it] = Farr.get(it).floatValue();
            }
            graf.addValores(floatArr, 2);

            Farr = ((ServicoStats) stats.get(s)).getCompmedio();
            floatArr = new float[Farr.size()];
            for (int it = 0; it < Farr.size(); it++) {
                floatArr[it] = Farr.get(it).floatValue();
            }
            graf.addValores(floatArr, 3);

            Farr = ((ServicoStats) stats.get(s)).getUtilizacao();
            floatArr = new float[Farr.size()];
            for (int it = 0; it < Farr.size(); it++) {
                floatArr[it] = Farr.get(it).floatValue();
            }
            graf.addValores(floatArr, 4);

            Farr = ((ServicoStats) stats.get(s)).getAtendidos();
            floatArr = new float[Farr.size()];
            for (int it = 0; it < Farr.size(); it++) {
                floatArr[it] = Farr.get(it).floatValue();
            }
            graf.addValores(floatArr, 5);


            graf.renderGrafico();
            BufferedImage myPicture = graf.scaledGraf(800, 600);
            JLabel picLabel = new JLabel(new ImageIcon(myPicture));
            picLabel.setSize(800, 600);
            picLabel.setVisible(true);

            jPanel3.add(picLabel);
            jPanel3.revalidate();
            jPanel3.repaint();
        }
        //jScrollPane2
    }
    
    public void saveToFiles(){
        for (int f=0; f<grafs.size(); f++){
            grafs.get(f).saveTo("SimulX_graf"+f+".jpg");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        resultadosArea = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jPanel3 = new javax.swing.JPanel();
        maxch = new javax.swing.JCheckBox();
        sizech = new javax.swing.JCheckBox();
        compch = new javax.swing.JCheckBox();
        tempch = new javax.swing.JCheckBox();
        utilch = new javax.swing.JCheckBox();
        atench = new javax.swing.JCheckBox();
        rerunbut = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setName("Form"); // NOI18N

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        resultadosArea.setColumns(20);
        resultadosArea.setRows(5);
        resultadosArea.setName("resultadosArea"); // NOI18N
        jScrollPane1.setViewportView(resultadosArea);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 694, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
        );

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(smallsimulator.Main.class).getContext().getResourceMap(FrameResultados.class);
        jTabbedPane1.addTab(resourceMap.getString("jPanel1.TabConstraints.tabTitle"), jPanel1); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jPanel3.setDoubleBuffered(true);
        jPanel3.setName("jPanel3"); // NOI18N
        jScrollPane2.setViewportView(jPanel3);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 694, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanel2.TabConstraints.tabTitle"), jPanel2); // NOI18N

        maxch.setSelected(true);
        maxch.setText(resourceMap.getString("maxch.text")); // NOI18N
        maxch.setName("maxch"); // NOI18N

        sizech.setSelected(true);
        sizech.setText(resourceMap.getString("sizech.text")); // NOI18N
        sizech.setName("sizech"); // NOI18N

        compch.setSelected(true);
        compch.setText(resourceMap.getString("compch.text")); // NOI18N
        compch.setName("compch"); // NOI18N

        tempch.setSelected(true);
        tempch.setText(resourceMap.getString("tempch.text")); // NOI18N
        tempch.setName("tempch"); // NOI18N

        utilch.setSelected(true);
        utilch.setText(resourceMap.getString("utilch.text")); // NOI18N
        utilch.setName("utilch"); // NOI18N

        atench.setSelected(true);
        atench.setText(resourceMap.getString("atench.text")); // NOI18N
        atench.setName("atench"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(smallsimulator.Main.class).getContext().getActionMap(FrameResultados.class, this);
        rerunbut.setAction(actionMap.get("rerunbutClicked")); // NOI18N
        rerunbut.setText(resourceMap.getString("rerunbut.text")); // NOI18N
        rerunbut.setName("rerunbut"); // NOI18N

        jButton1.setAction(actionMap.get("guardaGrafClicked")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(maxch)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sizech)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(compch)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tempch)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(utilch)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(atench)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rerunbut)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addGap(73, 73, 73))
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 699, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(maxch)
                    .addComponent(sizech)
                    .addComponent(compch)
                    .addComponent(tempch)
                    .addComponent(utilch)
                    .addComponent(atench)
                    .addComponent(rerunbut)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void rerunbutClicked() {
        jPanel3.removeAll();
        resultadosArea.setText("");

        boolean[] marcados = new boolean[6];
        if (maxch.isSelected()) {
            marcados[0] = true;
        } else {
            marcados[0] = false;
        }

        if (sizech.isSelected()) {
            marcados[1] = true;
        } else {
            marcados[1] = false;
        }

        if (tempch.isSelected()) {
            marcados[2] = true;
        } else {
            marcados[2] = false;
        }

        if (compch.isSelected()) {
            marcados[3] = true;
        } else {
            marcados[3] = false;
        }

        if (utilch.isSelected()) {
            marcados[4] = true;
        } else {
            marcados[4] = false;
        }

        if (atench.isSelected()) {
            marcados[5] = true;
        } else {
            marcados[5] = false;
        }


        Simulador runner = new Simulador(simuList);
        runner.executa();

        resultadosArea.setText(runner.getRelat());

        ArrayList<ServicoStats> stats = runner.getSerStats();
        ArrayList<Servico> servs = runner.getServicosIds();

        String[] nn = new String[6];
        nn[0] = "Fila Máximo";
        nn[1] = "Fila Size";
        nn[2] = "Tempo Médio";
        nn[3] = "Comprimento Médio";
        nn[4] = "Utilização";
        nn[5] = "Atendidos";
        
        grafs.clear();

        for (int s = 0; s < servs.size(); s++) {
            ConstrucaoGrafico graf = new ConstrucaoGrafico("linhas");
            grafs.add(graf);
            graf.setTituloGrafico("Gráfico report serviço " + ((ServicoStats) stats.get(s)).getNomeServico());
            graf.setEixoXLabel("tempo");
            graf.setEixoYLabel("valor");
            int toPrint = 0;
            for (int m = 0; m < marcados.length; m++) {
                if (marcados[m]) {
                    toPrint++;
                }
            }
            String[] nnfiltered = new String[toPrint];
            toPrint = 0;
            for (int m = 0; m < marcados.length; m++) {
                if (marcados[m]) {
                    nnfiltered[toPrint] = nn[m];
                    toPrint++;
                }
            }
            graf.setnRegistos(toPrint, nnfiltered);

            ArrayList<Float> Farr;
            float[] floatArr;

            toPrint = 0;
            if (marcados[0]) {
                Farr = ((ServicoStats) stats.get(s)).getFilamax();
                floatArr = new float[Farr.size()];
                for (int it = 0; it < Farr.size(); it++) {
                    floatArr[it] = Farr.get(it).floatValue();
                }
                graf.addValores(floatArr, toPrint);
                toPrint++;
            }

            if (marcados[1]) {
                Farr = ((ServicoStats) stats.get(s)).getFila();
                floatArr = new float[Farr.size()];
                for (int it = 0; it < Farr.size(); it++) {
                    floatArr[it] = Farr.get(it).floatValue();
                }
                graf.addValores(floatArr, toPrint);
                toPrint++;
            }

            if (marcados[2]) {
                Farr = ((ServicoStats) stats.get(s)).getTempomed();
                floatArr = new float[Farr.size()];
                for (int it = 0; it < Farr.size(); it++) {
                    floatArr[it] = Farr.get(it).floatValue();
                }
                graf.addValores(floatArr, toPrint);
                toPrint++;
            }

            if (marcados[3]) {
                Farr = ((ServicoStats) stats.get(s)).getCompmedio();
                floatArr = new float[Farr.size()];
                for (int it = 0; it < Farr.size(); it++) {
                    floatArr[it] = Farr.get(it).floatValue();
                }
                graf.addValores(floatArr, toPrint);
                toPrint++;
            }

            if (marcados[4]) {
                Farr = ((ServicoStats) stats.get(s)).getUtilizacao();
                floatArr = new float[Farr.size()];
                for (int it = 0; it < Farr.size(); it++) {
                    floatArr[it] = Farr.get(it).floatValue();
                }
                graf.addValores(floatArr, toPrint);
                toPrint++;
            }

            if (marcados[5]) {
                Farr = ((ServicoStats) stats.get(s)).getAtendidos();
                floatArr = new float[Farr.size()];
                for (int it = 0; it < Farr.size(); it++) {
                    floatArr[it] = Farr.get(it).floatValue();
                }
                graf.addValores(floatArr, toPrint);
                toPrint++;
            }


            graf.renderGrafico();
            BufferedImage myPicture = graf.scaledGraf(800, 600);
            JLabel picLabel = new JLabel(new ImageIcon(myPicture));
            picLabel.setSize(800, 600);
            picLabel.setVisible(true);

            jPanel3.add(picLabel);
            jPanel3.revalidate();
            jPanel3.repaint();
        }
    }

    @Action
    public void guardaGrafClicked() {
        this.saveToFiles();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox atench;
    private javax.swing.JCheckBox compch;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JCheckBox maxch;
    private javax.swing.JButton rerunbut;
    private javax.swing.JTextArea resultadosArea;
    private javax.swing.JCheckBox sizech;
    private javax.swing.JCheckBox tempch;
    private javax.swing.JCheckBox utilch;
    // End of variables declaration//GEN-END:variables
}
